package com.fingard.dsp.bank.directbank.bea01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BEA01Balance extends BEA01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();

        BEA01Header tmpHeader = new BEA01Header();
        tmpHeader.SERVICE_CODE = "2204";
        while (p_req7506.readRecord()) {
            tmpHeader.TRAN_REF_NO = getQryTransID();

            RetAtsBalDetail detail = new RetAtsBalDetail();

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpHeader);
            xmlWriter.writeElementString("CCY", convertCurToBank(p_req7506.readedDetail.actCur));
            xmlWriter.writeElementString("ACCT_NO", p_req7506.readedDetail.actNum);
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                    String errMsg = tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                    if ("000000".equals(retCode)) {
                        detail.respCode = "0";
                        detail.respInfo = "";
                        detail.actNum = p_req7506.readedDetail.actNum;
                        detail.actCur = convertCurToATS(tmpBkReader.getFirstText("/BCDL/opRep/opResultSet/opResult/CCY_1"));
                        String currBal = tmpBkReader.getFirstText("/BCDL/opRep/opResultSet/opResult/LEDGER_BAL");
                        detail.currBal = StringHelper.hasAnyChar(currBal) ? StringHelper.amtDiv100RetZeroIfNullOrEmpty(currBal.replaceFirst("^0*", "")) : "0";
                        String availBal = tmpBkReader.getFirstText("/BCDL/opRep/opResultSet/opResult/ACTUAL_BAL");
                        detail.availBal = StringHelper.hasAnyChar(availBal) ? StringHelper.amtDiv100RetZeroIfNullOrEmpty(availBal.replaceFirst("^0*", "")) : "0";
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = errMsg;
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                    retAts.writeRetDetail(detail, p_req7506.isBatch);
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = retStrArr[1].toString();
                retAts.writeRetDetail(detail, p_req7506.isBatch);
            }
        }

        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            BEA01Header bankHeader = new BEA01Header();
            bankHeader.SERVICE_CODE = "2203";//交易代码
            bankHeader.TRAN_REF_NO = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("ACCT_NO", p_req7505.actNum);
            xmlWriter.writeElementString("CCY", convertCurToBank(p_req7505.actCur));
            xmlWriter.writeElementString("START_DATE", p_req7505.dateFrom);
            xmlWriter.writeElementString("END_DATE", p_req7505.dateTo);
            xmlWriter.writeElementString("reqTime", "", true);
            xmlWriter.writeElementString("CHECKPS", "", true);
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());
            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/BCDL/opRep/retCode");
                    String errMsg = tmpBkReader.getFirstText("/BCDL/opRep/errMsg");
                    if ("000000".equals(retCode)) {
                        while (tmpBkReader.read("/BCDL/opRep/opResultSet/opResult")) {
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("TRAN_DATE");
                            if (tmpETYDAT.length() == 8) {
                                tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                            } else {
                                tmpRetDetail.balDate = tmpETYDAT;
                            }
                            tmpRetDetail.actBal = StringHelper.amtDiv100RetZeroIfNullOrEmpty(tmpBkReader.readedXElement.getSingleTextNotNull("ACTUAL_BAL_AMT").replace("-", "").replaceFirst("^0*", ""));
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = errMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
